package com.synchornize;

public class Test_2 {
    public static void main(String[] args) {
        AA aa = new AA();
        Thread thread1=new Thread(aa);
        Thread thread2=new Thread(aa);
        Thread thread3=new Thread(aa);
        thread1.setName("t1");
        thread2.setName("t2");
        thread3.setName("t3");
        thread1.start();
        thread2.start();
        thread3.start();

    }
}

class AA implements Runnable {
    private static int money = 100000;


    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                if (money >= 1000) {
                    money -= 1000;
                    System.out.println(Thread.currentThread().getName()+"取走了1000,还剩" + money);
                } else {
                    System.out.println("余额不足");
                    break;
                }

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}


